package cn.myzqu.sign.user.server.api;


import cn.myzqu.sign.common.response.ResultData;
import cn.myzqu.sign.common.miniprogram.WeChatApp;
import cn.myzqu.sign.user.client.WechatClient;
import cn.myzqu.sign.user.server.entity.OsAccount;
import cn.myzqu.sign.user.server.entity.OsUser;
import cn.myzqu.sign.user.server.service.AccountService;
import cn.myzqu.sign.user.server.service.UserService;
import cn.myzqu.sign.user.server.utils.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 微信小程序api
 * @Author: 的川
 * @Date: 2019/1/4.
 */
@RestController
@RequestMapping("/wechat")
@Api("微信小程序相关api")
public class WeChatApi implements WechatClient {

    @Autowired
    private WeChatApp weChatApp;

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserService userService;

//    @Autowired
//    private OsMessageService messageService;

    /**
     * 获取微信openId
     * @param code
     * @return
     */
    @GetMapping("/openId")
    @ApiOperation(value = "获取微信openId",notes = "开放第三方服务使用")
    @Override
    public ResultData getOpenId(String code){
        String openId = weChatApp.getOpenId(code);
        return ResultData.success(openId);
    }


    /**
     * 微信注册
     * @param osUser
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("微信注册用户")
    public ResultData register(@RequestBody OsUser osUser){
        OsAccount osAccount ;
        //判断openId是否已经存在
        if(osUser.getOpenId()!=null){
            osAccount = accountService.selectByOpenId(osUser.getOpenId());
            if(osAccount!=null){
                return ResultData.error("该微信账号已经被注册!");
            }
        }
        if(userService.register(osUser)){
            //注册成功，下发token
            HashMap map = new HashMap();
            String accessToken = JWTUtil.createJWT(osUser.getId()+"" ,"wechat" );
            map.put("accessToken",accessToken);
            map.put("userInfo",osUser);
            //发送提醒给待签到人员
//            messageService.addSysMessage(osUser.getId()+"",
//                    "欢迎使用Yao签到","欢迎使用Yao签到微信小程序，您已经是我们的注册会员，为了后续使用，请前往个人中心完善您的个人信息，谢谢！");
            return ResultData.success(map);
        }
        return ResultData.error("注册失败!");
    }

    /**
     * 微信授权登录
     * @param code
     * @return
     */
    @GetMapping("/login")
    @ApiOperation("微信授权登录")
    public ResultData login(String code){
        //不能直接传递openId过来，因为openId非常容易泄露
        //通过微信code获取openId
        String openId = weChatApp.getOpenId(code);
        HashMap map = new HashMap();
        if(openId!=null){
            //根据openId从账号表获取账号信息
            OsUser osUser = userService.selectByOpenId(openId);
            if(osUser==null){
                //不存在该账号
                map.put("openId",openId);
                return ResultData.error(1001,"请先注册！",map);
            }
            //如果存在该账号，则登录成功，下发token
            String accessToken = JWTUtil.createJWT(osUser.getId()+"" ,"wechat" );
            map.put("accessToken",accessToken);
            map.put("openId",openId);
            map.put("userInfo",osUser);
            return ResultData.success(map);
        }
        return ResultData.error("微信授权登录发生错误！");
    }
}
